package cn.csc.module.product.controller.app.collect;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.security.core.annotations.PreAuthenticated;
import cn.csc.framework.web.core.util.WebFrameworkUtils;
import cn.csc.module.product.controller.admin.collect.vo.SpuCollectCreateReqVO;
import cn.csc.module.product.controller.admin.collect.vo.SpuCollectPageReqVO;
import cn.csc.module.product.controller.admin.collect.vo.SpuCollectRespVO;
import cn.csc.module.product.convert.collect.SpuCollectConvert;
import cn.csc.module.product.dal.dataobject.collect.SpuCollectDO;
import cn.csc.module.product.service.collect.SpuCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.math.BigInteger;

import static cn.csc.framework.common.pojo.CommonResult.success;

/**
 * @author: 云隐
 * @Date: 2024/01/09
 **/
@Api(tags = "用户APP - 商品收藏信息")
@RestController("appSpuCollectController")
@RequestMapping("/product/collect")
@Validated
@Slf4j
@Setter
public class AppSpuCollectController {

    @Autowired
    private SpuCollectService spuCollectService;


    @PostMapping("/addIn")
    @Operation(summary = "收藏商品", description = "收藏商品")
    @PreAuthenticated
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spuId", required = true, value = "商品ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "shopId", required = true, value = "店铺ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "userId", required = true, value = "用户ID", example = "1", dataTypeClass = BigInteger.class)
    })
    public CommonResult<BigInteger> collectCreateSpu(@Validated @RequestBody SpuCollectCreateReqVO createReqVO) {
        BigInteger id = spuCollectService.createSpuCollect(createReqVO);
        return success(id);
    }

    @PostMapping("/get")
    @Operation(summary = "查看收藏商品", description = "查看收藏商品")
    @PermitAll
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spuId", required = true, value = "商品ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "shopId", required = true, value = "店铺ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "userId", required = true, value = "用户ID", example = "1", dataTypeClass = BigInteger.class)
    })
    public CommonResult<SpuCollectDO> collectSpu(@Validated @RequestBody SpuCollectCreateReqVO createReqVO) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        if (userId != null)
            createReqVO.setUserId(new BigInteger(String.valueOf(userId)));
        SpuCollectDO spuCollectDO = spuCollectService.getSpuByOne(createReqVO);
        return success(spuCollectDO);
    }

    @PostMapping("/delete")
    @ApiOperation("取消收藏")
    @PreAuthenticated
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spuId", required = true, value = "商品ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "shopId", required = true, value = "店铺ID", example = "1", dataTypeClass = BigInteger.class),
            @ApiImplicitParam(name = "userId", required = true, value = "用户ID", example = "1", dataTypeClass = BigInteger.class)
    })
    public CommonResult<Boolean> deleteSpu(@Validated @RequestBody SpuCollectCreateReqVO createReqVO) {
        spuCollectService.delSpuCollect(createReqVO);
        return success(true);
    }

    /**
     * 查询收藏的商品
     *
     * @param pageVO
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("收藏的商品")
    @PreAuthenticated
    public CommonResult<PageResult<SpuCollectRespVO>> getSpuPage(SpuCollectPageReqVO pageVO) {
        PageResult<SpuCollectDO> pageResult = spuCollectService.getSpuCollectPage(pageVO);
        return success(SpuCollectConvert.INSTANCE.convertPage(pageResult));
    }
}
